package com.yanqu.road.server.manager.union.activitygroup;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.servercenter.CrossServerInfo;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class UnionActivityGroupLockTest {

    private static Logger logger = LogManager.getLogger(UnionActivityGroupLockTest.class.getName());

    // 内网测试配置：活动跨服ID只能从这里面随机【外网不配置就行】
    public static void handleTestCrossList(List<CrossServerInfo> list) {
        String testCrossBattleServerId = ConfigHelper.getValue("testCrossBattleServerId");
        if (!StringUtils.isNullOrEmpty(testCrossBattleServerId)) {
            List<Integer> selfCrossIdList = StringUtils.stringToIntegerList(testCrossBattleServerId, ";");
            List<CrossServerInfo> list2 = new ArrayList<>();
            for (CrossServerInfo crossServerInfo : list) {
                if (selfCrossIdList.contains(crossServerInfo.getCrossServerId())) {
                    list2.add(crossServerInfo);
                }
            }
            if (!list2.isEmpty()) {
                System.err.println("-------------------- [商会活动分组] 已开启 内网活动跨服ID限制 --------------------");
                logger.error("-------------------- [商会活动分组] 已开启 内网活动跨服ID限制 --------------------");
                list.clear();
                list.addAll(list2);
            }
        }
    }

    // 内网测试配置：活动只能读这几个区服的
    public static void handleTestServerList(Map<Integer, ActivityInfo> infoMap) {
        String testActivityServerId = ConfigHelper.getValue("testActivityServerId");
        if (!StringUtils.isNullOrEmpty(testActivityServerId)) {
            Set<Long> selfServerIdSet = new HashSet<>(StringUtils.stringToLongList(testActivityServerId, ";"));
            if (!selfServerIdSet.isEmpty()) {
                System.err.println("-------------------- [商会活动分组] 已开启 内网活动区服ID限制 --------------------");
                logger.error("-------------------- [商会活动分组] 已开启 内网活动区服ID限制 --------------------");
                for (ActivityInfo info : infoMap.values()) {
                    boolean find = false;
                    for (long tempId : info.getServerIdList()) {
                        if (selfServerIdSet.contains(tempId)) {
                            find = true;
                            break;
                        }
                    }
                    if (!find) {
                        infoMap.remove(info.getActivityId());
                    }
                }
            }
        }
    }


}
